import router from './router';
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css'; // progress bar style

import EnumRouter from '@/constants/EnumRouter';
import { auth } from '@/utils/T';
NProgress.configure({ showSpinner: false }); // NProgress Configuration

const { noCheckIsLoginRoutes } = window.ENV.login; // login config

const whiteList = [
  EnumRouter['404'],
  EnumRouter['403'],
  ...noCheckIsLoginRoutes
];

router.beforeEach((to, from, next) => {
  NProgress.start();
  if (auth.isLogin()) {
    /* 如果有权限或者是403 404页面*/
    if (auth.can(to.meta.permissionMark) || whiteList.includes(to.path)) {
      next();
    } else {
      /* 无权限*/
      next(EnumRouter['403']);
    }
  } else if (whiteList.includes(to.path)) {
    next();
  } else {
    next(`${EnumRouter.login}?redirect_uri=${to.path}`);
  }
  NProgress.done();
});

router.afterEach(() => {
  // finish progress bar
  NProgress.done();
});
